﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cherry.Agile.Scheduler
{
    /// <summary>
    /// BasicTaskBuilder
    /// </summary>
    /// <typeparam name="TJob">Job</typeparam>
    public abstract class BasicTaskBuilder<TJob> : ITaskBuilder where TJob : IJob, new()
    {
        protected virtual string JobName
        {
            get
            {
                return typeof(TJob).FullName;
            }
        }

        protected virtual string CalendarName { get { return typeof(TJob).FullName; } }

        protected virtual ICalendar Calendar
        {
            get
            {
                return null;
            }
        }

        protected virtual IJobDetail CreateJob()
        {
            var job = JobBuilder.Create<TJob>().WithIdentity(JobName, CalendarName).RequestRecovery(true).Build();
            //job.RequestsRecovery = true;
            return job;
        }

        protected abstract ITrigger CreateTrigger();

        public virtual void BuildUp(Quartz.IScheduler scheduler)
        {
            var job = CreateJob();
            var trigger = CreateTrigger();
            scheduler.ScheduleJob(job, trigger);
            if (Calendar != null)
                scheduler.AddCalendar(CalendarName, Calendar, false, false);
        }
    }
}
